typedef Predicate<T> = bool Function(T t);

class CollectionUtil {

  static int indexOf<T>(List<T> list, Predicate<T> predicate) {
    int index = -1;
    for (var t in list) {
      index++;
      if (predicate(t)) {
        return index;
      }
    }
    return -1;
  }

  static bool contain<T>(List<T> list, Predicate<T> predicate) {
    for (var t in list) {
      if (predicate(t)) return true;
    }
    return false;
  }

  static bool delete<T>(List<T> list, Predicate<T> predicate) {
    int index = indexOf(list, predicate);
    if (index >= 0) {
      list.removeAt(index);
      return true;
    }
    return false;
  }

  static T? find<T>(List<T> list, Predicate<T> predicate) {
    int index = indexOf(list, predicate);
    if (index >= 0) {
      return list.elementAt(index);
    }
    return null;
  }

  static List<T> filter<T>(List<T> list, Predicate<T> predicate) {
    var result = <T>[];
    for (var t in list) {
      if (predicate(t)) {
        result.add(t);
      }
    }
    return result;
  }
}